import wpath
from scripts.base import *

# ============== 第1个控制台窗口：brownie console


def main():
    # 部署工厂合约 和 业务合约m1
    f = current_project.UpgradeabilityProxyFactory.deploy(from_admin)
    m1 = current_project.MyContract_V1.deploy(from_admin)

    wpath.print_r(f"工厂合约地址:{f.address}")
    wpath.print_m(f"v1业务合约地址:{m1.address}")

    # 将 m1 合约绑定到工厂合约的代理主合约上
    f.createProxy(admin.address, m1.address, from_admin)

    # 代理主合约（AdminUpgradeabilityProxy）的地址，
    p = f.g_proxy()

    wpath.print_r(f"代理主合约地址:{p}")

    # 通过代理主合约的地址访问m1
    m1_p = current_project.MyContract_V1.at(p)  # 第1次使用at访问p
    m1_p.initialize(120, from_user)

    x = m1_p.x(from_user)
    wpath.print_y(f"v1业务合约的x值:{x}")

    # 先部署m2合约，然后将m2地址绑定到p上，实现m1升级到m2
    m2 = current_project.MyContract_V2.deploy(from_admin)

    wpath.print_m(f"v2业务合约地址:{m2.address}")

    result = {"f": f.address, "p": p, "m1": m1.address, "m2": m2.address}
    writeJson(result)
